package com.cellulam.framework.push.sms;

import com.cellulam.framework.core.base.ResourceId;
import com.cellulam.framework.core.exceptions.SysException;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import lombok.Data;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.util.List;
import java.util.Map;

@Data
public class SmsMessage<T> implements ResourceId<T> {

    private final T resourceId;

    /**
     * 来源
     */
    private final String source;

    private final String templateCode;

    private final Map<String, Object> params;

    private final List<String> mobiles;

    private final List<Long> userIds;

    private final String signName;

    private final boolean async;

    private SmsMessage(Builder<T> builder) {
        this.source = builder.source;
        this.resourceId = builder.resourceId;
        this.templateCode = builder.templateCode;
        this.params = builder.params;
        this.userIds = builder.userIds;
        this.mobiles = builder.mobiles;
        this.signName = builder.signName;
        this.async = builder.async;
    }

    public static class Builder<T> {

        private T resourceId;

        private final String templateCode;

        private final Map<String, Object> params;

        private final List<String> mobiles;

        private final List<Long> userIds;

        private String signName;

        private boolean async;

        public Builder(String templateCode) {
            this.templateCode = templateCode;
            this.params = Maps.newHashMap();
            this.mobiles = Lists.newArrayList();
            this.userIds = Lists.newArrayList();
            this.async = true;
        }

        /**
         * 来源
         */
        private String source;

        /**
         * 资源标识, 必传
         * @param resourceId
         * @return
         */
        public Builder resourceId(T resourceId) {
            this.resourceId = resourceId;
            return this;
        }

        /**
         * 来源,非必传
         * @param source
         * @return
         */
        public Builder source(String source) {
            this.source = source;
            return this;
        }

        /**
         * 添加短信模板参数
         * @param key
         * @param value
         * @return
         */
        public Builder addParam(String key, Object value) {
            this.params.put(key, value);
            return this;
        }

        /**
         * 添加接收手机号
         * @param mobile
         * @return
         */
        public Builder addReceiver(String mobile) {
            this.mobiles.add(mobile);
            return this;
        }

        /**
         * 添加接收用户ID
         * @param userId
         * @return
         */
        public Builder addReceiver(long userId) {
            this.userIds.add(userId);
            return this;
        }

        /**
         * 短信签名, 不传使用配置文件中默认配置
         * @param signName
         * @return
         */
        public Builder signName(String signName) {
            this.signName = signName;
            return this;
        }

        /**
         * 是否异步发送，默认为异步
         * @param async
         * @return
         */
        public Builder async(boolean async) {
            this.async = async;
            return this;
        }

        public SmsMessage build() {
            if(this.resourceId == null) {
                throw new SysException("资源标识不能为空");
            }
            if(StringUtils.isEmpty(this.templateCode)) {
                throw new SysException("短信模板标号不能为空");
            }
            if(CollectionUtils.isEmpty(userIds) && CollectionUtils.isEmpty(mobiles)) {
                throw new SysException("必须添加接收者");
            }
            return new SmsMessage(this);
        }
    }
}
